// pages/focus/focus.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    store:[],
    focus_store:[]
  },
  focus:function(param){
    // console.log(param.currentTarget.dataset.id);
    const db = wx.cloud.database();
    var new_store = this.data.store;
    var that = this;
    if(param.currentTarget.dataset.state == 0) {
      new_store.forEach((ele) => {
        if(ele.brandname == param.currentTarget.dataset.id) {
          ele.focus = 1;
        }
      });
    }else {
      new_store.forEach((ele) => {
        if(ele.brandname == param.currentTarget.dataset.id) {
          ele.focus = 0;
        }
      });
    }


    
    db.collection('first_template_data').doc('44e883245f67159601477a3f1e452625').update({
  		data:{
  			store:new_store
    	}
   	}).then(res=>{
    	that.setData({
        store:new_store
      });
      var new_focused_store = new_store.filter((item) => {
        return item.focus == 1;
      });
      that.setData({
        focus_store:new_focused_store
      });
   	});

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    const db = wx.cloud.database();
    db.collection("first_template_data").where({
      index:"focus"
    }).get({
      success:function(res){
        that.setData({
          store:res.data[0].store
        });
        var stores = that.data.store.filter((item) => {
          return item.focus == 1;
        });
        that.setData({
          focus_store:stores
        });
        console.log(taht.data.focus_store);
        

      }
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})